package com.sky.service.impl;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        //拷贝套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //将套餐信息插入套餐表中
        setmealMapper.add(setmeal);

        //获取插入的套餐的id
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }

        //插入套餐菜品关系表
        setmealDishMapper.add(setmealDishes);
    }

    @Override
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        Long count = setmealMapper.count(setmealPageQueryDTO);

        Integer start =(setmealPageQueryDTO.getPage() - 1) * setmealPageQueryDTO.getPageSize();

        List<SetmealVO> setmealVOList = setmealMapper.selectByPage(setmealPageQueryDTO, start);
        PageResult pageResult = new PageResult(count, setmealVOList);
        return pageResult;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        //封装套餐信息
        Setmeal setmeal = Setmeal
                .builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //通过id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        //通过id查询套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //封装套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        List<Long> ids=new ArrayList<>() ;
        ids.add(setmeal.getId());
        //删除套餐菜品关系
        setmealDishMapper.deleteBySetmealId(ids);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            setmealDishMapper.add(setmealDishes);
        }
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断当前套餐是否正在起售
        List<Integer> statusList = setmealMapper.selectStatusByIds(ids);
        if (statusList != null && statusList.size() > 0) {
           throw new DeletionNotAllowedException("当前套餐正在起售中，不能删除");
        }
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBySetmealId(ids);
    }
}
